package admin

import (
	"gitee.com/gopher2011/gin"
	"gitee.com/gopher2011/mall/app/dao"
	"gitee.com/gopher2011/mall/app/service"
	"net/http"
	"strconv"
)

// 首页轮播广告管理 /home/advertise
var SmsHomeAdvertise = &smsHomeAdvertiseApi{}

type smsHomeAdvertiseApi struct{}

// 添加广告 /create Post 参数: dao.SmsHomeAdvertiseReq
func (it *smsHomeAdvertiseApi) Create(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.SmsHomeAdvertiseReq
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsHomeAdvertise.Create(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "添加广告失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "添加广告成功!", "data": res})
	}
}

// 删除广告 /delete Post 参数: ids[]int64
func (it *smsHomeAdvertiseApi) Delete(r *gin.Context) {
	// 将请求参数解析到 ptr上
	ids := make([]int64, 0)
	array := r.PostFormArray("ids")
	for _, v := range array {
		id, _ := strconv.Atoi(v)
		ids = append(ids, int64(id))
	}
	res, err := service.SmsHomeAdvertise.Delete(ids)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "删除广告失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "删除广告成功!", "data": res})
	}
}

// 修改上下线状态 /update/status/:id Post 参数: id,status
func (it *smsHomeAdvertiseApi) UpdateStatus(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	form := r.PostForm("status")
	status, _ := strconv.Atoi(form)
	res, err := service.SmsHomeAdvertise.UpdateStatus(int64(id), int32(status))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改上下线状态失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "修改上下线状态成功!", "data": res})
	}
}

// 获取广告详情 /:id Get 参数: id
func (it *smsHomeAdvertiseApi) GetItem(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	res, err := service.SmsHomeAdvertise.GetItem(int64(id))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "获取广告详情失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "获取广告详情成功!", "data": res})
	}
}

// 修改广告 /update/:id Post 参数: id,dao.SmsHomeAdvertiseReq
func (it *smsHomeAdvertiseApi) Update(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	// 将请求参数解析到 ptr上
	var ptr dao.SmsHomeAdvertiseReq
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsHomeAdvertise.Update(int64(id), ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改广告失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "修改广告成功!", "data": res})
	}
}

// 分页查询广告 /list Get 参数: dao.SmsHomeAdvertiseStr
func (it *smsHomeAdvertiseApi) List(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.SmsHomeAdvertiseStr
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsHomeAdvertise.List(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "分页查询广告失败!"})
		r.Abort()
	} else {
		//list := make([]dao.SmsHomeAdvertise,0)
		//for _,v := range res {
		//	startTime ,_:= time.ParseInLocation("2006-01-02 15:04:05",v.StartTime,time.Local)
		//	endTime,_:= time.ParseInLocation("2006-01-02 15:04:05",v.EndTime,time.Local)
		//	u := dao.SmsHomeAdvertise{
		//		Id: v.Id,
		//		Name: v.Name,
		//		Types: v.Types,
		//		Pic: v.Pic,
		//		StartTime: startTime,
		//		EndTime: endTime,
		//		Status: v.Status,
		//		ClickCount: v.ClickCount,
		//		OrderCount: v.OrderCount,
		//		Url: v.Url,
		//		Note: v.Note,
		//		Sort: v.Sort,
		//	}
		//	list = append(list,u)
		//}
		//r.JSON(http.StatusOK, gin.H{"msg": "分页查询广告成功!","data":list})
		r.JSON(http.StatusOK, gin.H{"msg": "分页查询广告成功!", "data": res})
	}
}
